package com.moon.game.command.guest.fete;

import java.util.ArrayList;
import java.util.List;

import com.moon.game.clazz.PlayerItemsProto.PlayerItems.PlayerItem;
import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.manager.ItemMG;
import com.moon.game.resource.model.Item;
import com.moon.game.service.FeteService;
import com.moon.game.service.ItemService;
import com.moon.game.service.PlayerItemService;
import com.moon.game.service.PlayerService;
import com.moon.game.vos.ErrorMsg;

/**
 * @description 祭祀（祭品：装备,兽蛋）
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-8-4 下午4:46:49
 */
public class FeteItemCmd extends BaseCmd {

	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
			case STATU_INIT:
				doInit(req);
				break;
			case STATU_CHECK:
				doCheckSelf(req);
				break;
			case STATU_RETURN:
				doResponse(req);
				break;
			default:
				break;
		}
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null || ps.length < 3) {
			doResponsePsError(req);
			return;
		}
		
		Integer type = Integer.parseInt(ps[1]);
		if (type == null) {
			doResponsePsError(req);
			return;
		}
		req.setVO("type", type);
		String addItemNums = "";  
		int addMoney = 0;
		int addExp = 0;
		int addGold = 0;
		int feteItemId = 0;
		if (type.intValue() == 1) {
			//装备类
			Long playerItemId = Long.parseLong(ps[0]);
			PlayerItem playerItem = PlayerItemService.getPlayerItem(req.getPlayerId(), playerItemId);
			if (playerItem == null) {
				doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_HAVE_NO_THE_ITEM, "你要献祭的装备不存在！"));
				return;
			}
			if (playerItem.getItemId() > 999 && playerItem.getItemId() < 1010) {
				doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_SYS, "新手装备不能献祭！"));
				return;
			}
			if (playerItem.getIsUse()) {
				doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_DEL_USING, "你要献祭的装备正在被使用，请先卸下再进行献祭！"));
				return;
			}
			req.setVO("player_item_id", playerItemId);
			feteItemId = playerItem.getItemId();
		}else {
			Integer itemId = Integer.parseInt(ps[0]);
			final Item item = ItemMG.instance.getItem(itemId);
			if (item == null) {
				doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_RESOURCE, "找不到你献祭的资源！"));
				return;
			}
			feteItemId = itemId;
		}
		
		//查找要献祭的道具资源
		final Item item = ItemMG.instance.getItem(feteItemId);
		if (item == null) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_RESOURCE, "找不到你要献祭的资源！"));
			return;
		}
		
		
		//解析献祭获得道具
		addItemNums = FeteService.doGrantItems(item.getLevel(), feteItemId);
		List<Integer> addItemList = new ArrayList<Integer>();
		List<Integer> addNumList = new ArrayList<Integer>();
		List<Item> addItems = new ArrayList<Item>();
		int addRoom = 0;
		if (addItemNums != null && addItemNums.length() > 0) {
			String[] a = addItemNums.split("[|]");
			if (a != null) {
				for (int i = 0; i < a.length; i++) {
					String cell = a[i];
					if (cell != null) {
						String[] n = cell.split("[:]");
						if (n != null && n.length == 2) {
							int id = Integer.parseInt(n[0]);
							int num = Integer.parseInt(n[1]);
							addItemList.add(id);
							addNumList.add(num);
							Item addItem = new Item();
							addItem.setId(id);
							addItem.setNum(num);
							addRoom += num;
							addItems.add(addItem);
						}
					}
				}
			}
		}
		req.setVO("add_items", addItems);
		//解析献祭获得碎银
		addMoney = FeteService.doGrandMoney(item.getLevel(), feteItemId);
		req.setVO("add_money", addMoney);
		//解析献祭获得经验
		addExp = FeteService.doGrandExp(item.getLevel(), feteItemId);
		req.setVO("add_exp", addExp);
		//解析献祭获得金币
		addGold = FeteService.doGrandGold(feteItemId);
		req.setVO("add_gold", addGold);
		
		//返回消耗的祭品
		req.setVO("fete_item_id", feteItemId);
		
		
		Integer sure = Integer.parseInt(ps[2]);
		if (sure == null) {
			doResponsePsError(req);
			return;
		}
		if (sure != 1) {
			List<Integer> canAddItems = FeteService.canGrantItems(item.getLevel(), feteItemId);
			req.setVO("can_add_items", canAddItems);
			//待确认
			req.setVO(TAG_ACK, true);
			response(req, req.getClientResponse());
			return;
		}
		
		boolean canAdd = ItemService.checkRoom(req.getSid(), req.getPlayerId(), addRoom);
		if (canAdd) {
			int ret = -1;
			if (type.intValue() == 1) {
				ret = PlayerItemService.delPlayerItem(req.getPlayerId(), Long.parseLong(ps[0]));
			} else {
				ret = ItemService.delItem(getPlayerVo(req), Integer.parseInt(ps[0]), 1);
			}
			if (ret == Constants.DEL_ITEM_SUCC) {
				int retItem = ItemService.addItems(getPlayerVo(req), addItemList, addNumList, true);
				switch (retItem) {
					case Constants.ADD_ITEM_NO_ROOM:
						doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_SYS, "献祭装备后添加祭品行囊空间不够！"));
						return;
					case Constants.ADD_ITEM_SUCC:
						break;
					case Constants.ADD_ITEM_FAIL:
						doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_SYS, "献祭装备后添加祭品异常！"));
						return;
				}
			}else {
				doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_HAVE_NO_THE_ITEM, "你要献祭的装备已经不存在了！"));
				return;
			}
		}else {
			doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_ROOM, "献祭" + item.getName() + "你行囊空间需要 "+ addRoom +"，你的行囊空间不够！"));
			return;
		}
		
		String sid = req.getSid();
		String pid = req.getPlayerId();
		
		if (addGold > 0) {
			PlayerService.doAddGold(sid, pid, addGold);
		}
		if (addMoney > 0) {
			PlayerService.doAddMoney(sid, pid, addMoney);
		}
		if (addExp > 0) {
			PlayerService.doAddExp(sid, pid, addExp);
		}
		response(req, req.getClientResponse());
	}

}
